# Maintainer: Naveen M K <naveen521kk at gmail.com>

_realname=skia
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=94.0.4606.71
# blindly follow chromium version number
# see https://omahaproxy.appspot.com/all.json?os=win&channel=stable
# for the commit
_commit="a71591a1efcc901dbebdb196d7a3c6e09364cf1f"
pkgrel=2
pkgdesc="Skia is a complete 2D graphic library for drawing Text, Geometries, and Images. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://skia.org"
license=("BSD")
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-python"
    "${MINGW_PACKAGE_PREFIX}-ninja"
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-pkg-config"
    "git"
)
depends=(
    "${MINGW_PACKAGE_PREFIX}-expat"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libwebp"
    "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
    "${MINGW_PACKAGE_PREFIX}-harfbuzz"
    "${MINGW_PACKAGE_PREFIX}-icu"
)
source=("gn::git+https://gn.googlesource.com/gn#tag=d62642c920e6a0d1756316d225a90fd6faa9e21e"
    "${_realname}-${pkgver}.tar.gz::https://github.com/google/skia/archive/${_commit}.zip"
    "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=fd86d60f33cbc794537c4da2ef7e298d7f81138e"
    "0001-add-mingw-toolchain.patch"
    "0002-gn-ninja-deletes-objs-workaround.patch"
    "0003-dont-download-prebuild-gn.patch"
    "0004-add-mingw-toolchain-skia.patch"
    "0005-fix-dwrite-function-mingw.patch"
    "0006-add-missing-limits.patch"
    "0007-add-mingw-toolchain-build.patch"
    "0008-dont-undef-CLSID_WICImagingFactory-on-mingw.patch"
    "0009-remove-as-needed-from-ldflags.patch"
    "harfbuzz.gn"
    "icu.gn"
    "zlib.gn"
    "skia.pc"
)
sha256sums=('SKIP'
            '4aa2ef73ccde55d47ee453dcf809cd801cac9ce9187ae28032a356e06cd1521d'
            'SKIP'
            '63d759de8bad9b8ac8879f8e69c24e62bf71fe14396cbdd190d48cabf8335800'
            'b831576af25b275fca72851ec275c9d21bee0be29b4eb973a3e2a2580deac003'
            'b035a9bbe7fc39a397a628a6ae00a7dbaed66f06c2cbb3a18275641597fe5b7f'
            '997ddb770ee5bdd0388c9cbf013714940a43a5b6c6733f376e14c3d04b1b110f'
            '93921aa3cfe5773a3122d1bb8011e153f30c361e89af00cdf4161e39dfe2c2bf'
            'd41ea264f389a34b3030ab5fb39107475a0e913123802dee241a20e1e5ecb1a5'
            '63f35c12168fcb5f025db2cb3a4bc693cb4546ce561dc95e45d10c1579daf786'
            '220d0c209c7291169be59dfab216e802f2adb8294e16b6aa65065718cf2259f9'
            '5d9998609ec6098401e395c8afaaddce52105c999489f27c906259b5cb47ce90'
            '7bd0b8cff2f1ec6822c74b731664007f598c3b442718060c499df7ba19141563'
            '36d32b737f87a676f5c4da9b7ae14193e6c1913ddb6508b979effdba2b80ea83'
            'b99874ea904724911528a5530c14f3051c358805bd36df9d205df31458ebf8b9'
            'a5dd30a1b53609aad427945fd119709a899d23a2e9606fbb676c146fdd04e3cd')

apply_patch_with_msg() {
    for _patch in "$@"; do
        msg2 "Applying $_patch"
        patch -Nbp1 -i "${srcdir}/$_patch"
    done
}

prepare() {
    mv "${srcdir}/skia-${_commit}" "${srcdir}/skia"

    echo "Syncing all dependencies"
    cd "${srcdir}/skia"
    apply_patch_with_msg 0003-dont-download-prebuild-gn.patch
    python ${srcdir}/skia/tools/git-sync-deps

    echo "Preparing GN source"
    cd "${srcdir}"/gn
    apply_patch_with_msg 0002-gn-ninja-deletes-objs-workaround.patch
    apply_patch_with_msg 0006-add-missing-limits.patch
    apply_patch_with_msg 0009-remove-as-needed-from-ldflags.patch
    python build/gen.py --out-path=out/${CARCH} --platform=mingw

    echo "Patching build directory"
    cd "${srcdir}"/build
    apply_patch_with_msg 0007-add-mingw-toolchain-build.patch
    #python "${srcdir}"/build/util/lastchange.py -o "${srcdir}"/build/util/LASTCHANGE
    rm -rf "${srcdir}"/skia/build
    ln -sf "${srcdir}"/build "${srcdir}"/skia
    echo "checkout_google_benchmark = false" >"${srcdir}"/skia/build/config/gclient_args.gni

    echo "Preparing Skia Source"
    cd "${srcdir}"/skia
    apply_patch_with_msg 0004-add-mingw-toolchain-skia.patch
    apply_patch_with_msg 0005-fix-dwrite-function-mingw.patch
    apply_patch_with_msg 0008-dont-undef-CLSID_WICImagingFactory-on-mingw.patch
    cp "${srcdir}/harfbuzz.gn" "${srcdir}/skia/third_party/harfbuzz/BUILD.gn"
    cp "${srcdir}/icu.gn" "${srcdir}/skia/third_party/icu/BUILD.gn"
    cp "${srcdir}/zlib.gn" "${srcdir}/skia/third_party/zlib/BUILD.gn"

    sed "s/@VERSION@/$pkgver/g" -i "$srcdir/skia.pc"
    sed "s/@CFLAGS@//g" -i "$srcdir/skia.pc"
    sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/skia.pc"
}

build() {
    local BUILDTYPE=Release
    if check_option "debug" "y"; then
        BUILDTYPE="Debug"
    fi

    case ${CARCH} in
    i686)
        _arch=x86
        ;;
    x86_64)
        _arch=x64
        ;;
    esac
    case ${BUILDTYPE} in
    Release)
        _is_official=true
        ;;
    Debug)
        _is_official=false
        ;;
    esac

    case ${MSYSTEM} in
    CLANG64|CLANG32)
        export CXX=clang++
        export CC=clang
        export LD=clang++
        export AR=llvm-ar
    ;;
    *)
        export CXX=g++
        export CC=gcc
        export LD=g++
        export AR=ar
    ;;
    esac

    export DEPOT_TOOLS_WIN_TOOLCHAIN=0
    cd "${srcdir}"/gn
    ninja -C out/${CARCH}

    cd "${srcdir}"/skia
    "${srcdir}"/gn/out/${CARCH}/gn gen "${PWD}/out/${BUILDTYPE}-${CARCH}" --args="target_cpu=\"${_arch}\" is_official_build=${_is_official} cc=\"${CC}\" cxx=\"${CXX}\" is_component_build=false skia_enable_tools=false skia_use_system_libjpeg_turbo=true skia_use_system_libwebp=true skia_use_system_libpng=true use_sysroot=false skia_use_xps=false"
    ninja -C "${PWD}/out/${BUILDTYPE}-${CARCH}"
}

package() {
    cd "skia"
    local BUILDTYPE=Release
    if check_option "debug" "y"; then
        BUILDTYPE="Debug"
    fi
    ls "${PWD}/out/${BUILDTYPE}-${CARCH}"

    install -d "$pkgdir"/${MINGW_PREFIX}/lib
    install -Dm755 out/${BUILDTYPE}-${CARCH}/*.a "$pkgdir"/${MINGW_PREFIX}/lib/

    install -d "$pkgdir"/${MINGW_PREFIX}/include/skia
    cp --parents `find ./include -name \*.h*` "$pkgdir"/${MINGW_PREFIX}/include/skia

    install -d "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig
    install -m644 "$srcdir/skia.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/

    install -d "$pkgdir"/${MINGW_PREFIX}/share/licenses/skia
    install -m644 LICENSE* "$pkgdir"/${MINGW_PREFIX}/share/licenses/${_realname}
}
